package com.ljs.myquartz;

import com.ljs.myquartz.testWays.SimpleJob1;
import com.ljs.myquartz.testWays.SimpleJob2;
import org.quartz.*;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//@Component
public class CronSchedulerJob {
    @Resource
    SchedulerFactoryBean schedulerFactoryBean;//获取Scheduler的工厂
    /*执行第一个工作的方法*/
    private void scheduleJob1(Scheduler scheduler) throws SchedulerException {
        //工作详情，工作的类，工作名称，组名
        JobDetail jobDetail= JobBuilder.newJob(SimpleJob1.class).withIdentity("job1","jobgruop1").build();
        //cron表达式 每隔10秒执行一次
        CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        //触发器，建立触发器，加入触发器名，组名，工作数据，进程 最后建立
        CronTrigger cronTrigger=TriggerBuilder.newTrigger()
                .withIdentity("trigger1","trigggroup1").usingJobData("name","ljs")
                .withSchedule(cronScheduleBuilder).build();
        //通过调度器来调度工作详情，触发器
        scheduler.scheduleJob(jobDetail,cronTrigger);
    }
    /*执行第二个任务*/
    private void scheduleJob2(Scheduler scheduler) throws SchedulerException {
        JobDetail jobDetail=JobBuilder.newJob(SimpleJob2.class).withIdentity("job2","group2").build();
      CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule("*/5 31 21 * * ?");//表达式
        //触发器，实体，job数据 ，进程，
      CronTrigger cronTrigger=TriggerBuilder.newTrigger()
              .withIdentity("trigger","triggergroup2")
              .usingJobData("name","jjj")
              .withSchedule(cronScheduleBuilder).build();

        scheduler.scheduleJob(jobDetail,cronTrigger);
    }
    //执行1
    public  void run1() throws SchedulerException {
        Scheduler scheduler=schedulerFactoryBean.getScheduler();
        scheduleJob1(scheduler);//调度job1
        System.out.println("调度job1");

    }
    //执行1
    public  void run2() throws SchedulerException {
        Scheduler scheduler=schedulerFactoryBean.getScheduler();
        scheduleJob2(scheduler);//调度job1
        System.out.println("调度job2");
    }
}
